<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>评论管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/x-admin/font.css">
    <link rel="stylesheet" href="/static/css/x-admin/xadmin.css">
    <link rel="stylesheet" href="/static/css/admin-index.css">
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>控制台</a>
        <a>文章管理</a>
        <a><cite>目录管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-sm layui-btn-radius" style="line-height:1.6em;margin-top:4px;float:right"
       href="javascript:location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-anim layui-anim-scale x-body">
    <div>
        <a href="/admin/cate/detail?cateType=newCate" class="layui-btn">新建目录</a>
    </div>
    <table class="layui-table">
        <thead>
        <tr>
            <th>ID</th>
            <th>排序</th>
            <th>名称</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="categorys">
        </tbody>
    </table>
</div>
<script type="text/javascript" src="/static/js/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/x-admin/xadmin.js"></script>
<script type="text/javascript">
    // 初始化分类目录
    function loadCategory() {
        var categorys = "";
        $.ajax({
            url: "/admin/cate/cateAll",
            type: "post",
            dataType: "json",
            success: function (data) {
                var list = data['data'].categoryList;
                buildOptions(list);
                $('#categorys').html(categorys);
                setDisabled(list);
            }, error: function () {
                layer.msg("初始化分类目录失败");
            }
        });

        function setDisabled(list) {
            if (list != null) {
                for (var i = 0; i < list.length; i++) {
                    var cate = list[i];
                    var parent = $("#cate-" + cate.cateParentId + "");
                    parent.attr('class', 'layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled');
                    parent.attr('onClick', '');
                    if (cate.children.length > 0) {
                        setDisabled(cate.children);
                    }
                }
            }
        }

        function buildOptions(list) {
            if (list === null || list.length < 0) {
                return;
            }
            for (var i = 0; i < list.length; i++) {
                var cate = list[i];
                var space = "";
                for (var j = 1; j < cate.index; j++) {
                    space += "&nbsp;&nbsp;";
                }
                categorys += "<tr>" +
                        "<td>" + cate.cateId + "</td>" +
                        "<td>" + cate.cateOrder + "</td>" +
                        "<td>" + space + "LV" + cate.index + "." + cate.cateName + "</td>" +
                        "<td>" + "<button id=\"cate-" + cate.cateId + "\" class=\"layui-btn layui-btn-danger layui-btn-xs\" onclick=\"del(this," + cate.cateId + ")\">删除</button>" +
                        "<button class=\"layui-btn layui-btn-xs\" onclick=\"update(" + cate.cateId + ")\">修改</button></tr>";
                if (cate.children.length > 0) {
                    buildOptions(cate.children);
                }
            }
        }
    }

    loadCategory();

    /*删除*/
    function del(obj, id) {
        layer.confirm('确认要删除吗？', {
            icon: 3,
            anim: 2
        }, function () {
            $.ajax({
                url: "/admin/cate/deleteCategory",
                data: {
                    cateId: id
                },
                type: "post",
                success: function (data) {
                    if (data.code === 2007) {
                        $(obj).parents("tr").remove();
                        layer.msg(data.message, {
                            icon: 1, time: 1000,
                            anim: 2
                        });
                    } else {
                        layer.msg(data.message, {
                            icon: 5, time: 1000,
                            anim: 2
                        });
                    }
                },
                error: function () {
                    layer.msg('目录删除失败!', {
                        icon: 5, time: 1000,
                        anim: 2
                    });
                }
            });
        });
    }

    function update(id) {
        location.href = "/admin/cate/detail?cateId=" + id + "&cateType=updateCate";
    }
</script>
</body>
</html>